//
//  PluPackageManagement.m
//  X-Touch2
//
//  Created by shengchao yang on 12-6-11.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "PluPackageManagement.h"
#import "PluPackageTableViewCell.h"
#import "PopoverTableViewController.h"
#import "FileManager.h"
#import "Common.h"
#import "Singleton.h"

@interface PluPackageManagement ()

@end

@implementation PluPackageManagement

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (IBAction)buttonClicked:(id)sender{
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    NSInteger bid = [sender tag];
    UIButton *btn = (UIButton *)[[self.view viewWithTag:TagOfUIView + 9] viewWithTag:bid];
    if (bid == TagOfUIButton + 10) {
        MyNSLog(@"添加[%f,%f,%f,%f]",btn.frame.origin.x,btn.frame.origin.y,btn.frame.size.width,btn.frame.size.height);
        PopoverTableViewController *ptvc = [[PopoverTableViewController alloc] initWithNibName:@"PopoverTableViewController" bundle:nil popoverType:4];//4-增加套餐明细查询
        [ptvc PopOverFromRect:btn.frame inView:[self.view viewWithTag:TagOfUIView + 9] directions:UIPopoverArrowDirectionUp];
        [ptvc release];  
    }else if (bid == TagOfUIButton + 11){
        MyNSLog(@"关闭");
        [self.view removeFromSuperview];
    }
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return AppSession.myOrderPluPkgs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";
	PluPackageTableViewCell *cell = (PluPackageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil){
		cell = [[[PluPackageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
	}    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;//设置选择cell时，没有任何背景颜色
    cell.tag = TagOfUITableViewCell + indexPath.row;
    MyOrderPluPkgs * tempMyOrderPkg = (MyOrderPluPkgs *)[AppSession.myOrderPluPkgs objectAtIndex:indexPath.row];
    [cell setData:tempMyOrderPkg index:indexPath.row+1];
    return cell;
}

//刷新界面
-(void) refreshPluPkgTable{
    [AppSession initMyOrderPluPkgs:AppSession.currentMyOrderDish.ppCode];//重新查询套餐明细
    [plupkgTable reloadData];
    
    //计算套餐明细总价
    float result = 0;
    for(MyOrderPluPkgs *tempPkg in AppSession.myOrderPluPkgs){
        result += [tempPkg.pkgNumber floatValue] * [tempPkg.pkgPrice floatValue];
    }
    lblPkgTotal.text = [NSString stringWithFormat:@"￥%0.2f", result];
}
#pragma mark - View lifecycle
- (void)viewDidLoad{
    [super viewDidLoad];
    addNObserver(@selector(refreshPluPkgTable), @"SysMsg_PluPackageManagement_refreshPluPkgTable");
    [btnAddDetail setTitle:TranslateStr(@"增加明细") forState:UIControlStateNormal];
    plupkgTable = (UITableView *)[self.view viewWithTag:TagOfUITableView];
    
    lblPluName = (UILabel *)[self.view viewWithTag:TagOfUILabel + 20];
    lblPluPrice = (UILabel *)[self.view viewWithTag:TagOfUILabel + 21];
    lblPkgTotal = (UILabel *)[self.view viewWithTag:TagOfUILabel + 22];
    lblPluName.text = [NSString stringWithFormat:@"%@ [%@] %@",TranslateStr(@"套餐"), JsonField(AppSession.currentMyOrderDish.modDescripts), TranslateStr(@"明细")];
    float price = [AppSession.currentMyOrderDish.modPrice floatValue];
    lblPluPrice.text = [NSString stringWithFormat:@"￥%0.2f", price];
    
    [((UILabel *)[[self.view viewWithTag:TagOfUIView + 9] viewWithTag:TagOfUILabel + 40]) setText:TranslateStr(@"明细总价")];
    [((UILabel *)[[self.view viewWithTag:TagOfUIView + 9] viewWithTag:TagOfUILabel + 41]) setText:TranslateStr(@"套餐单价")];
    [((UILabel *)[[self.view viewWithTag:TagOfUIView + 9] viewWithTag:TagOfUILabel + 23]) setText:TranslateStr(@"数量")];
    [((UILabel *)[[self.view viewWithTag:TagOfUIView + 9] viewWithTag:TagOfUILabel + 24]) setText:TranslateStr(@"单价")];
    [((UILabel *)[[self.view viewWithTag:TagOfUIView + 9] viewWithTag:TagOfUILabel + 44]) setText:TranslateStr(@"菜名")];
    [((UILabel *)[[self.view viewWithTag:TagOfUIView + 9] viewWithTag:TagOfUILabel + 45]) setText:TranslateStr(@"序号")];
    
    //计算套餐明细总价
    float result = 0;
    for(MyOrderPluPkgs *tempPkg in AppSession.myOrderPluPkgs){
        result += [tempPkg.pkgNumber floatValue] * [tempPkg.pkgPrice floatValue];
    }
    lblPkgTotal.text = [NSString stringWithFormat:@"￥%0.2f", result];
}
- (void)viewDidUnload{
    [btnAddDetail release];
    btnAddDetail = nil;
    [super viewDidUnload];
}
-(void)dealloc{
    MyNSLog(@"PluPackageManagement is deallocing");
    removeNObserver(@"SysMsg_PluPackageManagement_refreshPluPkgTable");
    [btnAddDetail release];
    [super dealloc];
}
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
@end
